package com.zimmor.hq.util;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.UnsupportedEncodingException;
import java.util.Arrays;

public class IdCardEncodeUtil {

    public static final byte ENTER_FRAME_PREFIX = 33;
    public static final byte EXIT_FRAME_PREFIX = 120;
    public static final byte STDERR_FRAME_PREFIX = 50;
    public static final byte STDIN_FRAME_PREFIX = 45;
    public static final byte STDIN_REQUEST_FRAME_PREFIX = 95;
    public static final byte STDOUT_FRAME_PREFIX = 49;
    public static final byte NETWORK_TYPE_2G = 2;
    public static final byte NETWORK_TYPE_3G = 3;
    public static final byte NETWORK_TYPE_4G = 4;
    public static final byte NETWORK_TYPE_UNCONNECTED = -1;
    public static final byte NETWORK_TYPE_UNKNOWN = 0;
    public static final byte NETWORK_TYPE_WIFI = 1;
    public static final byte MESSAGE_TYPE_CONNACK = 2;
    public static final byte MESSAGE_TYPE_CONNECT = 1;
    public static final byte MESSAGE_TYPE_DISCONNECT = 14;
    public static final byte MESSAGE_TYPE_PINGREQ = 12;
    public static final byte MESSAGE_TYPE_PINGRESP = 13;
    public static final byte MESSAGE_TYPE_PUBACK = 4;
    public static final byte MESSAGE_TYPE_PUBCOMP = 7;
    public static final byte MESSAGE_TYPE_PUBLISH = 3;
    public static final byte MESSAGE_TYPE_PUBREC = 5;
    public static final byte MESSAGE_TYPE_PUBREL = 6;
    public static final byte MESSAGE_TYPE_SUBACK = 9;
    public static final byte MESSAGE_TYPE_SUBSCRIBE = 8;
    public static final byte MESSAGE_TYPE_UNSUBACK = 11;
    public static final byte MESSAGE_TYPE_UNSUBSCRIBE = 10;

    public static String encodeIdCardNum(String idCardNum) {
//        String str = "130221197703182319";
        String str2 = "88d2979416892a02e4636eed5f4e0f98";
        byte[] b = new byte[0];
        try {
            b = b(idCardNum.getBytes("utf-8"), a(str2));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
//        System.out.println(Arrays.toString(b));
        String result = a(b);
        return result;
    }


    public static byte[] b(byte[] arr_b, byte[] arr_b1) {
        if(arr_b != null && arr_b1 != null) {
            try {
                b b0 = new b();
                b0.c = true;
                b0.a = 1;
                IdCardEncodeUtil a0 = new IdCardEncodeUtil();
                a0.c(b0, arr_b1);
                return a0.a(b0, arr_b);
            }
            catch(Exception exception0) {
                exception0.printStackTrace();
            }
        }

        return null;
    }


//    public static void main(String[] args) throws UnsupportedEncodingException {
//        String str = "130221197703182319";
//        String str2 = "88d2979416892a02e4636eed5f4e0f98";
//        byte[] b = b(str.getBytes("utf-8"), a(str2));
//        System.out.println(Arrays.toString(b));
//        String result = a(b);
//        System.out.println(result);
//    }
    public static byte[] a(String str) {
        if (str == null || str.equals("")) {
            return null;
        }
        String upperCase = str.toUpperCase();
        int length = upperCase.length() / 2;
        char[] charArray = upperCase.toCharArray();
        byte[] bArr = new byte[length];
        for (int i2 = 0; i2 < length; i2++) {
            int i3 = i2 * 2;
            bArr[i2] = (byte) (a(charArray[i3 + 1]) | (a(charArray[i3]) << 4));
        }
        return bArr;
    }

    public static byte a(char c2) {
        return (byte) "0123456789ABCDEF".indexOf(c2);
    }
    public static String a(byte[] arr_b) {
        if(arr_b != null) {
            StringBuilder stringBuilder0 = new StringBuilder();
            for(int v = 0; v < arr_b.length; ++v) {
                String s = Integer.toHexString(arr_b[v] & 0xFF);
                if(s.length() == 1) {
                    stringBuilder0.append("0");
                }

                stringBuilder0.append(s);
            }

            return stringBuilder0.toString().toUpperCase();
        }

        throw new IllegalArgumentException("Argument b ( byte array ) is null! ");
    }



    public static class b {
        public int a;
        public long[] b;
        public boolean c;
    
        public b() {
            this.a = 1;
            this.c = true;
            this.b = new long[0x20];
        }
    }
    

    public static final byte[] a;
    public static final int[] b;
    public static final int[] c;

    static {
        a = new byte[]{-42, (byte)0x90, -23, -2, -52, (byte)0xE1, 61, -73, 22, -74, 20, -62, 40, -5, 44, 5, 43, 103, -102, 0x76, 42, -66, 4, -61, -86, 68, 19, 38, 73, (byte)0x86, 6, -103, -100, 66, 80, -12, (byte)0x91, -17, -104, 0x7A, 51, 84, 11, 67, -19, -49, -84, 98, -28, -77, 28, -87, -55, 8, -24, -107, (byte)0x80, -33, -108, -6, 0x75, (byte)0x8F, 0x3F, -90, 71, 7, -89, -4, -13, 0x73, 23, -70, (byte)0x83, 89, 60, 25, -26, (byte)0x85, 0x4F, -88, 104, 107, (byte)0x81, -78, 0x71, 100, -38, (byte)0x8B, -8, -21, 15, 75, 0x70, 86, -99, 53, 30, 36, 14, 94, 99, 88, (byte)0xD1, -94, 37, 34, 0x7C, 59, 1, 33, 120, (byte)0x87, -44, 0, 70, 87, -97, -45, 39, 82, 76, 54, 2, -25, (byte)0xA0, -60, -56, -98, -22, -65, (byte)0x8A, -46, 0x40, -57, 56, -75, -93, -9, -14, -50, -7, 97, 21, (byte)0xA1, (byte)0xE0, -82, 93, -92, -101, 52, 26, 85, -83, -109, 50, 0x30, -11, (byte)0x8C, (byte)0xB1, -29, 29, -10, -30, 46, (byte)0x82, 102, -54, 0x60, (byte)0xC0, 41, 35, -85, 13, 83, 78, 0x6F, -43, -37, 55, 69, -34, -3, (byte)0x8E, 0x2F, 3, -1, 106, 0x72, 109, 108, 91, 81, (byte)0x8D, 27, -81, -110, -69, -35, -68, 0x7F, 17, -39, 92, 65, 0x1F, 16, 90, -40, 10, (byte)0xC1, 49, -120, -91, -51, 0x7B, -67, 45, 0x74, (byte)0xD0, 18, -72, -27, -76, -80, (byte)0x89, 105, -105, 74, 12, -106, 0x77, 0x7E, 101, -71, -15, 9, -59, 110, -58, (byte)0x84, 24, -16, 0x7D, -20, 58, -36, 77, 0x20, 0x79, -18, 0x5F, 62, -41, -53, 57, 72};
        b = new int[]{-1548633402, 0x56AA3350, 0x677D9197, 0xB27022DC};
        c = new int[]{0x70E15, 472066609, 0x383F464D, 0x545B6269, 0x70777E85, 0x8C939AA1, 0xA8AFB6BD, 0xC4CBD2D9, 0xE0E7EEF5, 0xFC030A11, 0x181F262D, 0x343B4249, 1347903077, 1819507329, -2003855715, 0xA4ABB2B9, -1060647211, 0xDCE3EAF1, 0xF8FF060D, 0x141B2229, 0x30373E45, 1280531041, 0x686F767D, -2071227751, 0xA0A7AEB5, 0xBCC3CAD1, 0xD8DFE6ED, 0xF4FB0209, 269950501, 0x2C333A41, 1213159005, 0x646B7279};
    }

    public final byte a(byte b) {
        return a[b & 0xFF];
    }

    public final long a(long v) {
        byte[] arr_b = new byte[4];
        byte[] arr_b1 = new byte[4];
        this.a(v, arr_b, 0);
        arr_b1[0] = this.a(arr_b[0]);
        arr_b1[1] = this.a(arr_b[1]);
        arr_b1[2] = this.a(arr_b[2]);
        arr_b1[3] = this.a(arr_b[3]);
        long v1 = this.a(arr_b1, 0);
        return this.a(v1, 23) ^ (this.a(v1, 13) ^ v1);
    }

    public final long a(long v, int v1) {
        return v >> 0x20 - v1 | v << v1;
    }

    public final long a(long v, long v1, long v2, long v3, long v4) {
        return v ^ this.b(v1 ^ v2 ^ v3 ^ v4);
    }

    public final long a(byte[] arr_b, int v) {
        return ((long)(arr_b[v + 3] & 0xFF)) & 0xFFFFFFFFL | (((long)(arr_b[v] & 0xFF)) << 24 | ((long)((arr_b[v + 1] & 0xFF) << 16)) | ((long)((arr_b[v + 2] & 0xFF) << 8)));
    }

    public final void a(long v, byte[] arr_b, int v1) {
        arr_b[v1] = (byte)(((int)(v >> 24 & 0xFFL)));
        arr_b[v1 + 1] = (byte)(((int)(v >> 16 & 0xFFL)));
        arr_b[v1 + 2] = (byte)(((int)(v >> 8 & 0xFFL)));
        arr_b[v1 + 3] = (byte)(((int)(v & 0xFFL)));
    }

    public final void a(long[] arr_v, int v) {
        long v1 = arr_v[v];
        int v2 = 0x1F - v;
        arr_v[v] = arr_v[v2];
        arr_v[v2] = v1;
    }

    public final void a(long[] arr_v, byte[] arr_b) {
        long[] arr_v1 = new long[36];
        long v = this.a(arr_b, 0);
        long v1 = this.a(arr_b, 4);
        long v2 = this.a(arr_b, 8);
        long v3 = this.a(arr_b, 12);
        arr_v1[0] = v ^ ((long)b[0]);
        arr_v1[1] = v1 ^ ((long)b[1]);
        arr_v1[2] = v2 ^ ((long)b[2]);
        arr_v1[3] = v3 ^ ((long)b[3]);
        for(int v4 = 0; v4 < 0x20; ++v4) {
            arr_v1[v4 + 4] = arr_v1[v4] ^ this.a(arr_v1[v4 + 1] ^ arr_v1[v4 + 2] ^ arr_v1[v4 + 3] ^ ((long)c[v4]));
            arr_v[v4] = arr_v1[v4 + 4];
        }
    }

    public final void a(long[] arr_v, byte[] arr_b, byte[] arr_b1) {
        long[] arr_v1 = new long[36];
        arr_v1[0] = this.a(arr_b, 0);
        arr_v1[1] = this.a(arr_b, 4);
        arr_v1[2] = this.a(arr_b, 8);
        arr_v1[3] = this.a(arr_b, 12);
        for(int v = 0; v < 0x20; ++v) {
            arr_v1[v + 4] = this.a(arr_v1[v], arr_v1[v + 1], arr_v1[v + 2], arr_v1[v + 3], arr_v[v]);
        }

        this.a(arr_v1[35], arr_b1, 0);
        this.a(arr_v1[34], arr_b1, 4);
        this.a(arr_v1[33], arr_b1, 8);
        this.a(arr_v1[0x20], arr_b1, 12);
    }

    public byte[] a(b b0, byte[] arr_b) throws Exception {
        if(arr_b != null) {
            if((b0.c) && b0.a == 1) {
                arr_b = this.b(arr_b, 1);
            }

            int v = arr_b.length;
            ByteArrayInputStream byteArrayInputStream0 = new ByteArrayInputStream(arr_b);
            ByteArrayOutputStream byteArrayOutputStream0 = new ByteArrayOutputStream();
            while(v > 0) {
                byte[] arr_b1 = new byte[16];
                byte[] arr_b2 = new byte[16];
                byteArrayInputStream0.read(arr_b1);
                this.a(b0.b, arr_b1, arr_b2);
                byteArrayOutputStream0.write(arr_b2);
                v += -16;
            }

            byte[] arr_b3 = byteArrayOutputStream0.toByteArray();
            if((b0.c) && b0.a == 0) {
                arr_b3 = this.b(arr_b3, 0);
            }

            byteArrayInputStream0.close();
            byteArrayOutputStream0.close();
            return arr_b3;
        }

        throw new Exception("input is null!");
    }

    public final long b(long v) {
        byte[] arr_b = new byte[4];
        byte[] arr_b1 = new byte[4];
        this.a(v, arr_b, 0);
        arr_b1[0] = this.a(arr_b[0]);
        arr_b1[1] = this.a(arr_b[1]);
        arr_b1[2] = this.a(arr_b[2]);
        arr_b1[3] = this.a(arr_b[3]);
        long v1 = this.a(arr_b1, 0);
        return this.a(v1, 2) ^ v1 ^ this.a(v1, 10) ^ this.a(v1, 18) ^ this.a(v1, 24);
    }

    public final long b(long v, int v1) {
        return v << v1;
    }

    public void b(b b0, byte[] arr_b) throws Exception {
        if(b0 != null) {
            if(arr_b != null && arr_b.length == 16) {
                b0.a = 0;
                this.a(b0.b, arr_b);
                for(int v = 0; v < 16; ++v) {
                    this.a(b0.b, v);
                }

                return;
            }

            throw new Exception("key error!");
        }

        throw new Exception("ctx is null!");
    }

    public final byte[] b(byte[] arr_b, int v) {
        byte[] arr_b1;
        if(arr_b == null) {
            return null;
        }

        if(v == 1) {
            int v2 = 16 - arr_b.length % 16;
            arr_b1 = new byte[arr_b.length + v2];
            System.arraycopy(arr_b, 0, arr_b1, 0, arr_b.length);
            for(int v1 = 0; v1 < v2; ++v1) {
                arr_b1[arr_b.length + v1] = (byte)v2;
            }
        }
        else {
            int v3 = arr_b[arr_b.length - 1];
            arr_b1 = new byte[arr_b.length - v3];
            System.arraycopy(arr_b, 0, arr_b1, 0, arr_b.length - v3);
        }

        return arr_b1;
    }

    public void c(b b0, byte[] arr_b) throws Exception {
        if(b0 != null) {
            if(arr_b != null && arr_b.length == 16) {
                b0.a = 1;
                this.a(b0.b, arr_b);
                return;
            }

            throw new Exception("key error!");
        }

        throw new Exception("ctx is null!");
    }
}

